home *** CD-ROM | disk | FTP | other *** search
/ PC Plus SuperCD (UK) 1999 October / pcp156b.iso / handson / files / altprog / PCPlus Smalltalk Tutorial.st
Encoding:
Text File  |  1999-06-04  |  6.0 KB  |  170 lines

  1. "PC Plus Dolphin Smalltalk Tutorial - Part One"
  2.  
  3. "Note: You must evaluate ([CTRL]-E) or Display ([CTRL]-D) each line
  4. of code in this document in the order in which they appear. If you don't do this,
  5. objects required by code later in the document may not have been 
  6. created, so that some examples won't run."
  7.  
  8. " --- CLASSES, OBJECTS AND MESSAGES --- "
  9. "In Smalltalk, everything is an Object - for example, the number 100, and the word 'Hello' are
  10. both Objects. And every object belongs to a class. You can ask an Object to identify itself.
  11. Display (that is, press [CTRL-D] on the line containing) each of these expressions: "
  12.  
  13. 100 class.
  14. 1000000000000 class.
  15. 'Hello' class.
  16. #(a b c ) class.
  17. Smalltalk class.
  18.  
  19. "In the examples above, 'class' is a message. In Smalltalk, you get things done by sending messages to 
  20. Objects. Some messages, such as 'class', take no arguments. Others take other objects are arguments. For example, the String message 'at:' takes an integer argument, whereas the number message '*' takes another 
  21. number as an argument. Display these:"
  22.  
  23. 2 * 10
  24. 3 * 3
  25. 'Smalltalk is wonderful!' at: 1
  26. 'Smalltalk is wonderful!' at: 2
  27.  
  28. "Some messages have more than one part. For example, copyFrom:to: takes two arguments, one
  29. after the first part, copyFrom:, another after the second part, to:. Make sure to place a colon at the end
  30. of these messages. Try out the following:"
  31.  
  32. 'Smalltalk is wonderful!' copyFrom: 1 to: 9
  33. 'Smalltalk is wonderful!' copyFrom: 1 to: 3 * 3
  34.  
  35.  
  36. " --- FROM OOP TO OOPS! - THE SMALLTALK DEBUGGER --- "
  37. "Try out these deliberate bugs. When you see the error dialog, select Debug. Take a look at the
  38. information in the debugger then close it to return to this document."
  39.  
  40. 'Smalltalk is wonderful!' copyFrom: 1 to: 90
  41. 123456789 copyFrom: 1 to: 9
  42. '20' squared.
  43.  
  44.  
  45. " --- VARIABLES AND SCOPE --- "
  46. "There are various sorts of variable, each of which has a different scope.
  47. A workspace variable lives inside the environs of a workspace such as this window.
  48. Evaluate the following:"
  49.  
  50. i := 10.
  51. i.
  52. i class.
  53.  
  54. "In the above code, Smalltalk created a SmallInteger object called i. But it can re-use
  55. this variable name and re-create it as an object of some other type. Display the
  56. following lines to see this in practice:"
  57.  
  58. i := 'Hello from the PC Plus Smalltalk Tutorial Workspace!'
  59. i.
  60. i class.
  61.  
  62. "Now open a new workspace by selecting the File menu, then New.
  63. Enter i into your second workspace and display it. It won't recognise the
  64. i variable. 
  65.  
  66. Now switch back to this workspace. Evaluate the following:"
  67.  
  68. Hello := 'Hello from the world of Smalltalk!'
  69.  
  70. "When asked if you want to define Hello as a global variable, click Yes.
  71. Now Display this:"
  72.  
  73. Hello.
  74.  
  75. "Switch to the 2nd Workspace, enter Hello and Display it.
  76. This time, it knows all about the Hello variable. That's because a variable
  77. name that starts with a capital letter, such as Hello, is global. It is known
  78. to the entire Smalltalk system. A variable that starts with a small letter, 
  79. such as i is only known in its own Workspace.
  80.  
  81. Ask Smalltalk to remove Global Variables once you've finished with them. 
  82. Send the removeKey message to Smalltalk, passing to it the name of the Global
  83. variable, preceded by a hash (#) symbol. Evaluate the following:"
  84.  
  85. Smalltalk removeKey: #Hello.
  86.  
  87. "Check that the Hello variable has been removed. Try to Display the following:"
  88. Hello.
  89.  
  90. "Assigning values.
  91. Display each of the following, one line at a time."
  92.  
  93. x := 'Hello'.
  94. y := 'world'.
  95. z := x , ' ' , y.
  96. z.
  97.  
  98. "Note the effect of the comma in the above. It joins together or 'concatenates' the strings"
  99.  
  100. "--- ARRAYS ---"
  101. "Examples of some arrays. Display each of them."
  102. strlist := #('one' 'two' 'three').
  103. intlist := #(1 2 3).#(1 2 3).
  104. mixedlist := #( 'one' 2 (3 4 5) $6 ).
  105.  
  106. "What type of object is at subscript 3 of this array....?"
  107. (mixedlist at: 3) class.
  108.  
  109. "Let's put a different object at subscript 2."
  110. mixedlist at: 2 put: strlist.
  111. mixedlist.
  112.  
  113. "Recall how we used a comma to concatenate strings earlier. Now try it out with arrays.
  114. Display the following:"
  115. #(1 2 3 ), #(4 5 6).
  116. strAndIntList := strlist , intlist.
  117. strAndIntList.
  118.  
  119. "--- CASCADING MESSAGES ---"
  120. "Evaluate these two lines ([CTRL-E]). Make sure the System Transcript window is visible"
  121. ob := strlist at: 2.
  122. Transcript cr. Transcript show: ob.
  123.  
  124. "In the above example, we have written two statements, each ending with a full stop, each
  125. passing a single message (cr and show:) to Transcript. We can actually send multiple
  126. or 'cascading' messages to the same receiver object (here Transcript) by separating
  127. each message with a semi-colon. Evaluate the following:"
  128. Transcript cr; show: ob.
  129.  
  130. "--- USE THE printString MESSAGE TO DISPLAY DATA ---"
  131. "Now evaluate each of these lines one by one"
  132. Transcript cr; show: 10.                            "you'll have a problem showing an integer."
  133.                       "click the Terminate button when prompted"    
  134. Transcript cr; show: 10 printString.            "you can print an integer as a String though"
  135.  
  136. Transcript cr; show: 10 class printString.    "you can print a class as a String too"
  137.  
  138. 10 timesRepeat: [Transcript show: 'Hello'; cr].
  139.  
  140. "Now mark off and evaluate this entire code block (be sure to include the first line: | a |.
  141. Note: You must have evaluated mixedlist earlier in this document in order for this to work."
  142. |  a  |
  143. a := 1.
  144. mixedlist size timesRepeat: [ 
  145.   ob := mixedlist at: a.
  146.  Transcript cr; show: '[', a printString , '] ', 
  147.         ob printString, 
  148.         ' is an object of the Class: ', 
  149.         ob class printString.
  150.   a := a + 1.
  151. ].
  152.  
  153. "In the above, a is a temporary variable. It is discarded after the code has been run. Verify it by attempting to display a:"
  154.  
  155. a.
  156.  
  157. "Temporary variables are indicated by enclosing them between vertical bars. 
  158. Mark off this code block and Display it. Note that
  159. the ^ symbol at the end tells Smalltalk to Return the value following it."
  160.  
  161. | temp1 temp2 temp3 |
  162.   temp1 := 10.
  163.   temp2 := '...Hello '.
  164.   temp3 := 'world'.
  165.   ^temp1 printString, temp2, temp3.
  166.  
  167. "Now evaluate this:"
  168. temp1.
  169.  
  170.